package cn.bone.ou.community.service.impl;

import cn.bone.base.annotation.MyLogger;
import cn.bone.base.auth.JWTInfo;
import cn.bone.base.bean.CommonResult;
import cn.bone.base.bean.DataGrid;
import cn.bone.base.bean.Ref;
import cn.bone.base.utils.TimeUtil;
import cn.bone.ou.community.bean.House;
import cn.bone.ou.community.mapper.HouseMapper;
import cn.bone.ou.community.param.HouseParam;
import cn.bone.ou.community.service.IHouseService;
import cn.bone.ou.community.vo.HouseVo;
import cn.bone.ou.user.bean.User;
import cn.bone.ou.user.mapper.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class HouseServiceImpl implements IHouseService {

    @Resource
    private HouseMapper houseMapper;
    @Resource
    private UserMapper userMapper;

    /**获取房屋列表*/
    public CommonResult<DataGrid<HouseVo.ListVo>> list(HouseParam.ListParam param){
        List<House> list = houseMapper.list(param.getCommunityId(),
                param.getStart(), param.getNum(),
                param.getBuildNum(), param.getUnitNum(),
                param.getHouseNum(), param.getIsAuth());
        int count = houseMapper.countList(param.getCommunityId(),
                param.getBuildNum(), param.getUnitNum(),
                param.getHouseNum(), param.getIsAuth());

        List<HouseVo.ListVo> vos = list.stream().map(house -> {
            HouseVo.ListVo vo = new HouseVo.ListVo();
            vo.setHouseId(house.getId());
            vo.setCommunityId(house.getCommunityId());
            vo.setBuildNum(house.getBuildNum());
            vo.setUnitNum(house.getUnitNum());
            vo.setHouseNum(house.getHouseNum());
            vo.setSaleArea(house.getSaleArea());
            vo.setRealArea(house.getRealArea());
            vo.setHouseType(house.getHouseType());
            vo.setIsAuth(house.getIsAuth());
            vo.setUsable(house.getUsable());
            vo.setCreateTime(TimeUtil.formaterYYYYMMddHHmm(house.getCreateTime()));
            return vo;
        }).collect(Collectors.toList());
        DataGrid<HouseVo.ListVo> dataGrid = new DataGrid<>();
        dataGrid.setRows(vos);
        dataGrid.setTotal(count);
        return Ref.ok(dataGrid);
    }

    /**添加房屋*/
    @MyLogger(msg = "添加房屋")
    public CommonResult add(HouseParam.AddParam param){
        House house = houseMapper.getByNum(param.getBuildNum(),
                param.getUnitNum(), param.getHouseNum(),param.getCommunityId());
        if(house!=null){
            return Ref.error("该房屋已存在");
        }
        JWTInfo jwtInfo = JWTInfo.get();

        house = new House();
        house.setCommunityId(param.getCommunityId());
        house.setBuildNum(param.getBuildNum());
        house.setUnitNum(param.getUnitNum());
        house.setHouseNum(param.getHouseNum());
        house.setSaleArea(param.getSaleArea());
        house.setRealArea(param.getRealArea());
        house.setHouseType(param.getHouseType());
        house.setIsAuth(0);
        house.setUsable(1);
        house.setCreateMemberId(jwtInfo.getUserId());
        house.setCreateTime(new Date());
        houseMapper.insert(house);
        return Ref.ok();
    }

    /**修改房屋*/
    @MyLogger(msg = "修改房屋")
    public CommonResult modify(HouseParam.ModifyParam param){
        House house = houseMapper.selectById(param.getHouseId());
        if(house==null){
            return Ref.error("错误的房屋编号");
        }
        JWTInfo jwtInfo = JWTInfo.get();

        house.setBuildNum(param.getBuildNum());
        house.setUnitNum(param.getUnitNum());
        house.setHouseNum(param.getHouseNum());
        house.setSaleArea(param.getSaleArea());
        house.setRealArea(param.getRealArea());
        house.setHouseType(param.getHouseType());
        house.setUpdateMemberId(jwtInfo.getUserId());
        house.setUpdateTime(new Date());
        houseMapper.updateById(house);
        return Ref.ok();
    }

    /**启用房屋*/
    @MyLogger(msg = "启用房屋")
    public CommonResult usable(HouseParam.UsableParam param){
        House house = houseMapper.selectById(param.getHouseId());
        if(house==null){
            return Ref.error("错误的房屋编号");
        }
        house.setUsable(1);
        houseMapper.updateById(house);
        return Ref.ok();
    }

    /**禁用房屋*/
    @MyLogger(msg = "禁用房屋")
    public CommonResult disable(HouseParam.DisableParam param){
        House house = houseMapper.selectById(param.getHouseId());
        if(house==null){
            return Ref.error("错误的房屋编号");
        }
        house.setUsable(0);
        houseMapper.updateById(house);
        return Ref.ok();
    }

    /**删除房屋*/
    @MyLogger(msg = "删除房屋")
    public CommonResult delete(HouseParam.DeleteParam param){
        House house = houseMapper.selectById(param.getHouseId());
        if(house==null){
            return Ref.error("错误的房屋编号");
        }
        if(house.getUsable()==1){
            return Ref.error("只有禁用状态才能删除");
        }
        List<User> users = userMapper.listByHouseId(param.getHouseId());
        if(users.size()>0){
            User user = users.get(0);
            String msg="";
            if(user.getUserType()==2){
                msg = "删除失败，该房屋已被住户["+user.getNickName()+"]绑定";
            }else if(user.getUserType()==3){
                msg = "删除失败，该房屋以被业主["+user.getRealName()+"]绑定";
            }
            return Ref.error(msg);
        }
        houseMapper.deleteById(house.getId());
        return Ref.ok();
    }

    /**批量导入*/
    @MyLogger(msg = "批量导入房屋")
    public CommonResult excelImport(List<HouseParam.ExcelImportParam> list,Integer communityId){

        JWTInfo jwtInfo = JWTInfo.get();
        Date time = new Date();
        int size = list.size();
        int ignore = 0;
        int insertNum = 0;
        for(HouseParam.ExcelImportParam p : list){
            int i = houseMapper.ignoreInsert(communityId, p.getBuildNum(), p.getUnitNum(), p.getHouseNum(),
                    p.getSaleArea(), p.getRealArea(), 1,
                    0, 1, jwtInfo.getUserId(), time);
            if(i==0){
                ignore = ignore + 1;
            }
        }
        insertNum = size - ignore;
        String msg = "成功导入["+insertNum+"]条数据，["+ignore+"]条重复数据被忽略";
        return Ref.okMsg(msg);
    }
}
